#ifndef ERR_HANDLE_H_H
#define ERR_HANDLE_H_H

#include <stdint.h>

#ifdef __cplusplus
extern "C"
{
#endif

#define ERR_GROUP_LEN 4

    typedef struct
    {
        uint8_t b_global;                   
        uint8_t b_found_edge;               
        uint8_t b_lost_edge;                
        uint8_t b_disp_switch_pulse;        
        uint8_t b_space_en;                 
        uint8_t b_space_st;                 
        uint16_t disp_code;                 
        uint32_t disp_err;                  
        uint16_t val[ERR_GROUP_LEN];        
        uint16_t old[ERR_GROUP_LEN];        
        uint16_t found_edge[ERR_GROUP_LEN]; 
        uint16_t lost_edge[ERR_GROUP_LEN];  
        uint16_t mask[ERR_GROUP_LEN];       
    } sys_err_type;

#define sys_err_make(group, pos) (((uint32_t)group<<16)|(1UL << pos))

#define sys_err_set(obj, err) obj.val[err >> 16] |=  (err & 0xFFFF)
#define sys_err_clr(obj, err) obj.val[err >> 16] &= ~(err & 0xFFFF)
#define sys_err_msk(obj, msk) obj.mask[msk >> 16] = msk & 0xFFFF

#define sys_err_get(obj, err) (obj.val[err >> 16] & (err & 0xFFFF))
#define sys_err_found_edge_get(obj, err) (obj.found_edge[err >> 16] & (err & 0xFFFF))
#define sys_err_lost_edge_get(obj, err) (obj.lost_edge[err >> 16] & (err & 0xFFFF))

    void err_init(sys_err_type *sys_err);
    void err_poll(sys_err_type *sys_err);

#ifdef __cplusplus
}
#endif

#endif 
